﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class SJWH_SJWHAdd : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!string.IsNullOrEmpty(CommonUse.GetID()) && !string.IsNullOrEmpty(GetSYQXZID()))
        InitData();
      //DDLManage.BindYQXZ(ddlYQXZ);
    }
  }

  protected string GetSYQXZID()
  {
    string sYQXZID = "";
    if (Request.QueryString["yqxzid"] != null)
    {
      sYQXZID = Request.QueryString["yqxzid"].ToString();
    }
    return sYQXZID;
  }

  protected void InitData()
  {
    string sSql = "select * from sjlr_bqql where cbsjid=@0 and yqxzid=@1";
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20), new SqlParameter("@1", SqlDbType.VarChar, 20) };
    cmdParams[0].Value = CommonUse.GetID();
    cmdParams[1].Value = GetSYQXZID();
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql, cmdParams);
    if (dr.HasRows)
    {
      dr.Read();
      txtName.Text = dr["bqql"].ToString();
    }
    dr.Close();
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    //if (string.IsNullOrEmpty(sID))
    //  Insert();
    //else
    Update();
  }

  //插入
  protected void Insert()
  {
    //if (ExistID())
    //{
    //  lbMessage.Text = "此日期已存在价格";
    //  return;
    //}
    //SqlParameter[] cmdParams = null;
    //string sSql = "";
    //List<CommandInfo> cis = new List<CommandInfo>();
    //CommandInfo ci;
    //string sID = GetID();

    //sSql = "insert into jcxx_yqjg values(@0,@1,@2,@3,@4)";
    //cmdParams = new SqlParameter[5] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 12),
    // new SqlParameter("@2", SqlDbType.VarChar, 12),new SqlParameter("@3", SqlDbType.VarChar, 12),
    // new SqlParameter("@4", SqlDbType.VarChar, 200)};
    //cmdParams[0].Value = sID;
    //cmdParams[1].Value = ddlYQXZ.SelectedValue;
    //cmdParams[2].Value = txtName.Text;
    //cmdParams[3].Value = dateJG.Text;
    //cmdParams[4].Value = txtBZ.Text;
    //ci = new CommandInfo();
    //ci.CommandText = sSql;
    //ci.Parameters = cmdParams;
    //cis.Add(ci);

    //if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    //{
    //  lbMessage.Text = "保存成功";
    //}
  }

  //判断此名称是否存在
  protected bool ExistID()
  {
    bool flag = false;
    //string sSql = "select jgid as num from jcxx_yqjg where jgrq>=@0 and yqxzid=@1";
    //SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20), new SqlParameter("@1", SqlDbType.VarChar, 20) };
    //cmdParams[0].Value = dateJG.Text;
    //cmdParams[1].Value = ddlYQXZ.SelectedValue;
    //SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql, cmdParams);
    //if (dr.HasRows)
    //{
    //  flag = true;
    //}
    //dr.Close();
    return flag;
  }

  protected string GetID()
  {
    string sID = "1";
    string sSql = "select isnull(max(jgid)+1,'1') id from jcxx_yqjg";
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql);
    if (dr.HasRows)
    {
      dr.Read();
      sID = dr["id"].ToString();
    }
    dr.Close();
    return sID;
  }

  //修改
  protected void Update()
  {
    //if (ExistID())
    //{
    //  lbMessage.Text = "此日期已存在价格";
    //  return;
    //}
    SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci;

    sSql = "update sjlr_bqql set bqql=@2 where CBSJid=@0 and yqxzid=@1";
    cmdParams = new SqlParameter[3] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10),
     new SqlParameter("@2", SqlDbType.VarChar, 14)};
    cmdParams[0].Value = CommonUse.GetID();
    cmdParams[1].Value = GetSYQXZID();
    cmdParams[2].Value = txtName.Text;
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    sSql = "update zhcx set cx_ysql=@2 where CBSJid=@0 and yqxzid=@1";
    cmdParams = new SqlParameter[3] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10),
     new SqlParameter("@2", SqlDbType.VarChar, 14)};
    cmdParams[0].Value = CommonUse.GetID();
    cmdParams[1].Value = GetSYQXZID();
    cmdParams[2].Value = txtName.Text;
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      lbMessage.Text = "保存成功";
    }
  }
}
